package Forms;

import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JButton;
import javax.swing.JViewport;
import Logic.DatabaseReader;

import relationtables.AirplaneClassTable;
import relationtables.BookingsTable;
import relationtables.ClassMenueTable;
import relationtables.ClassSeatsTable;
import relationtables.FlightTripTable;
import relationtables.UsersTable;

import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import datatables.AbstractTable;
import datatables.AirplanesTable;
import datatables.AirportsTable;
import datatables.CompaniesTable;
import datatables.FlightsTable;
import datatables.MenuesTable;
import datatables.PassengerClassTable;
import datatables.PassengersTable;
import datatables.SeatsTable;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class AdminFrame extends JFrame {

	private JPanel sqlPane;
	private JTabbedPane tablesPane;
	private AbstractTable selectedTable;
	private LoginPanel loginPanel;

	private JTabbedPane mainTabbedPane;
	private FlightsTable flightsTable;
	private AirportsTable airportsTable;
	private CompaniesTable companiesTable;
	private PassengerClassTable passengerClassTable;
	private MenuesTable menuesTable;
	private SeatsTable seatsTable;
	private AirplanesTable airplanesTable;
	private PassengersTable passengersTable;

	private JTabbedPane relationsPane;
	private AirplaneClassTable airplaneClassTable;
	private ClassMenueTable classMenueTable;
	private ClassSeatsTable classSeatsTable;
	private FlightTripTable flightTripTable;
	private BookingsTable bookingsTable;
	private UsersTable usersTable;

	private Connection con;
	private PreparedStatement prepStatement;
	private Statement statement;
	private ResultSet resultSet;
	private DatabaseReader databaseReader;

	/**
	 * Create the frame. The AdminFrame is a very simple database editor that
	 * lets an admin add all data he needs to the database he can also remove
	 * and change existing data
	 * 
	 */
	public AdminFrame(DatabaseReader _databaseReader) {
		// the constructor handle mostly the creation of the ui
		databaseReader = _databaseReader;
		if(databaseReader.isDatabaseConnected())
			con = databaseReader.getConnection();
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setResizable(false);
		setTitle("Administration Frame");
		setBounds(100, 100, 800, 600);

		loginPanel = new LoginPanel(this, "admin");

		sqlPane = new JPanel();
		sqlPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		sqlPane.setLayout(new BorderLayout(0, 0));
		setContentPane(loginPanel);

		mainTabbedPane = new JTabbedPane(JTabbedPane.TOP);
		addMainTables();
		addRelations();
		sqlPane.add(mainTabbedPane);

		JPanel buttonsPanel = new JPanel();
		sqlPane.add(buttonsPanel, BorderLayout.SOUTH);
		GridBagLayout gbl_buttonsPanel = new GridBagLayout();
		gbl_buttonsPanel.columnWidths = new int[] { 0, 30, 30, 30, 30, 30, 0,
				0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
		gbl_buttonsPanel.rowHeights = new int[] { 30 };
		gbl_buttonsPanel.columnWeights = new double[] { 0.0, 0.0, 0.0, 0.0,
				0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
				0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 };
		gbl_buttonsPanel.rowWeights = new double[] { 0.0 };
		buttonsPanel.setLayout(gbl_buttonsPanel);

		JButton addButton = new JButton("+");
		addButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
					showEntryAdder(getSelectedTable(mainTabbedPane
						.getSelectedIndex()));

			}
		});
		GridBagConstraints gbc_addButton = new GridBagConstraints();
		gbc_addButton.insets = new Insets(0, 0, 0, 5);
		gbc_addButton.gridx = 0;
		gbc_addButton.gridy = 0;
		buttonsPanel.add(addButton, gbc_addButton);

		JButton removeButton = new JButton("-");
		removeButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {

				deleteRow();

			}
		});
		GridBagConstraints gbc_removeButton = new GridBagConstraints();
		gbc_removeButton.insets = new Insets(0, 0, 0, 5);
		gbc_removeButton.gridx = 1;
		gbc_removeButton.gridy = 0;
		buttonsPanel.add(removeButton, gbc_removeButton);

		JButton exitButton = new JButton("Exit");
		exitButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				dispose();
			}
		});
		GridBagConstraints gbc_exitButton = new GridBagConstraints();
		gbc_exitButton.gridx = 23;
		gbc_exitButton.gridy = 0;
		buttonsPanel.add(exitButton, gbc_exitButton);
		this.setPreferredSize(new Dimension(800, 600));
		this.setVisible(true);

	}

	// prompts you before deleting the row fomr the selected table
	public void deleteRow() {

		int decision = JOptionPane.showConfirmDialog(this,
				"Do you really want to remove the selected row?");
		if (decision == JOptionPane.YES_OPTION)
			selectedTable.deleteRow();
	}

	// as the name says it gets the selected table
	public String getSelectedTable(int mainTabbedPaneIndex) {
		AbstractTable bufferTable;
		JScrollPane bufferPane;
		switch (mainTabbedPaneIndex) {
		case 0:
			bufferPane = (JScrollPane) tablesPane.getSelectedComponent();

			break;
		case 1:
			bufferPane = (JScrollPane) relationsPane.getSelectedComponent();
			break;
		default:
			bufferPane = null;
			break;
		}
		if (bufferPane != null) {
			JViewport port = (JViewport) bufferPane.getComponent(0);
			bufferTable = (AbstractTable) port.getComponent(0);
			selectedTable = bufferTable;
			return bufferTable.getTableName();
		} else {
			JOptionPane.showMessageDialog(this, "No table selected");
			return null;
		}
	}

	public void showEntryAdder(String tableName) {
		if (tableName != null) {
			new EntryAdderFrame(selectedTable, con);

		}
	}

	// if a connection is established to a db,it checks the credentials and then
	// if the user is an admin
	public void logOn(String userID, String pwd) {

		if (databaseReader.isDatabaseConnected()) {
			
			try {
				con = databaseReader.getConnection();
				prepStatement = con
						.prepareStatement("SELECT * FROM \"users\" WHERE \"passengerid\" = ? AND password = ?");
				prepStatement.setString(1, userID);
				prepStatement.setString(2, pwd);
				resultSet = prepStatement.executeQuery();

				if (resultSet.next()) {
					if (resultSet.getBoolean(3)) {
						System.out.println(resultSet.getMetaData()
								.getColumnName(3));
						setContentPane(sqlPane);
						repaint();
						pack();
					} else {
						JOptionPane.showMessageDialog(this, userID
								+ " is not and admin");

					}
				} else {
					JOptionPane.showMessageDialog(this,
							"User or Password incorrect");

				}

			} catch (SQLException e) {
				e.printStackTrace();
				JOptionPane.showMessageDialog(this,
						"User or Password incorrect");

			}
		} else {
			JOptionPane.showMessageDialog(this, "No Database connected!");
		}

	}

	// main tables added
	public void addMainTables() {
		tablesPane = new JTabbedPane(JTabbedPane.TOP);

		mainTabbedPane.addTab("Datatables", null, tablesPane, null);

		flightsTable = new FlightsTable(con);
		tablesPane.addTab("Flights", null, new JScrollPane(flightsTable), null);

		airportsTable = new AirportsTable(con);
		tablesPane.addTab("Airports", null, new JScrollPane(airportsTable),
				null);

		companiesTable = new CompaniesTable(con);
		tablesPane.addTab("Companies", null, new JScrollPane(companiesTable),
				null);

		passengerClassTable = new PassengerClassTable(con);
		tablesPane.addTab("Passenger Classes", null, new JScrollPane(
				passengerClassTable), null);

		menuesTable = new MenuesTable(con);
		tablesPane.addTab("Menues", null, new JScrollPane(menuesTable), null);

		seatsTable = new SeatsTable(con);
		tablesPane.addTab("Seats", null, new JScrollPane(seatsTable), null);

		airplanesTable = new AirplanesTable(con);
		tablesPane.addTab("Airplanes", null, new JScrollPane(airplanesTable),
				null);

		passengersTable = new PassengersTable(con);
		tablesPane.addTab("Passengers", null, new JScrollPane(passengersTable),
				null);

		tablesPane.setSelectedIndex(0);
	}

	// relations added
	public void addRelations() {
		relationsPane = new JTabbedPane(JTabbedPane.TOP);

		mainTabbedPane.addTab("Relations", null, relationsPane, null);

		flightTripTable = new FlightTripTable(con);
		relationsPane.addTab("Flight-Trips", null, new JScrollPane(
				flightTripTable), null);

		classSeatsTable = new ClassSeatsTable(con);
		relationsPane.addTab("Class-Seats", null, new JScrollPane(
				classSeatsTable), null);

		classMenueTable = new ClassMenueTable(con);
		relationsPane.addTab("Class-Menue", null, new JScrollPane(
				classMenueTable), null);

		airplaneClassTable = new AirplaneClassTable(con);
		relationsPane.addTab("Airplane-Classes", null, new JScrollPane(
				airplaneClassTable), null);

		bookingsTable = new BookingsTable(con);
		relationsPane.addTab("Bookings", null, new JScrollPane(bookingsTable),
				null);
		usersTable = new UsersTable(con);
		relationsPane.addTab("Users", null, new JScrollPane(usersTable), null);
	}

}
